package com.jk.admin.service;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jk.admin.dao.CartDao;
import com.jk.admin.entity.Cart;
import com.jk.admin.entity.Product;
import com.jk.admin.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartService {

    @Autowired
    private CartDao cartDao;

    @Autowired
    private RedisTemplate redisTemplate;


    public List<Cart> queryCartList(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        Gson gson = new Gson();
        HashOperations hashOperations = redisTemplate.opsForHash();
        String cart = (String) hashOperations.get("cart", user.getUser_id());
        List<Cart> list= gson.fromJson(cart, new TypeToken<List<Cart>>() {}.getType());
        return list;
    }

    public void addCart(HttpServletRequest request,String id,Integer num) {
        User user = (User) request.getSession().getAttribute("user");
        Gson gson = new Gson();
        Boolean has = redisTemplate.opsForHash().hasKey("cart",user.getUser_id());
        /*此处需要es查询商品信息*/
        Product product = cartDao.queryById(id);
        Cart cart = new Cart();
        cart.setProductId(product.getProduct_id());
        cart.setProductName(product.getProduct_name());
        cart.setProductPrice(product.getProduct_price());
        cart.setProductImage(product.getProduct_imgurl());
        cart.setUserId(user.getUser_id());
        cart.setNum(num);
        if (user!=null){
            if (has){
                List<Cart> cartList = new ArrayList<Cart>();
                String cart1 = (String) redisTemplate.opsForHash().get("cart", cart.getUserId());
                cartList = gson.fromJson(cart1, new TypeToken<List<Cart>>() {}.getType());
                cartList.add(cart);
                Map<String, Object> map = new HashMap<>();
                map.put(cart.getUserId(),gson.toJson(cartList));
                redisTemplate.opsForHash().putAll("cart",map);
            }else {
                List<Cart> list = new ArrayList<Cart>();
                list.add(cart);
                Map<String, Object> map = new HashMap<>();
                map.put(cart.getUserId(),gson.toJson(list));
                redisTemplate.opsForHash().putAll("cart",map);
                }
        }else{
            List<Cart> cartList = new ArrayList<Cart>();
            cartList.add(cart);
            request.getSession().setAttribute("cart",gson.toJson(cartList));
        }



    }

    public void add(Cart cart) {
        Boolean has = redisTemplate.opsForHash().hasKey("cart",cart.getUserId());
        Gson gson = new Gson();
        if (has){
            List<Cart> listPro = new ArrayList<Cart>();
            String cart1 = (String) redisTemplate.opsForHash().get("cart", cart.getUserId());
            listPro = gson.fromJson(cart1, new TypeToken<List<Cart>>() {
            }.getType());
            listPro.add(cart);
            Map<String, Object> map = new HashMap<>();
            map.put(cart.getUserId(),gson.toJson(listPro));
            redisTemplate.opsForHash().putAll("cart",map);

        }else {
            List<Cart> list = new ArrayList<Cart>();
            list.add(cart);
            Map<String, Object> map = new HashMap<>();
            map.put(cart.getUserId(),gson.toJson(list));
            redisTemplate.opsForHash().putAll("cart",map);
        }
    }

    public void del(String id,String productId) {
        String cart = (String) redisTemplate.opsForHash().get("cart", id);
        List<Cart> cartList = new ArrayList<Cart>();
        Gson gson = new Gson();
        cartList = gson.fromJson(cart, new TypeToken<List<Cart>>() {}.getType());
        for (int i = 0; i <cartList.size() ; i++) {
            if(productId.equals(cartList.get(i).getProductId())){
                cartList.remove(i);
            }
        }
        redisTemplate.opsForHash().delete("cart",id);
        Map<String, Object> map = new HashMap<>();
        map.put(id,gson.toJson(cartList));
        redisTemplate.opsForHash().putAll("cart",map);
    }
}

